# THIS SCRIPT CODES AND CLEANS DATA FROM THE ANES CUMULATIVE FILE. IT NEEDS TO BE 
# USED IN CONJUNCTION WITH THE FILE (AVAILABLE ON DATAVERSE) ENTITLED "ANES-1988.dta"
library(foreign)
library(car)
library(readstata13)
library(ggplot2)
library(stargazer)
library(grid)
library(gridExtra)
library(survey)
library(dplyr)
library(lemon)
library(ggpubr)
library(reshape2)
#-----
# READ IN THE DATA. SELECT THE FILE "ANES-1988.dta"
our.data = read.dta13(file.choose(), nonint.factors = TRUE,
                      generate.factors = TRUE)
#------
# FEELING THERMOMETER, REPUBLICAN PARTY. CORRECT CODE IS V880165
table(our.data$V880165)
levels(our.data$V880165)
our.data$repFT = our.data$V880165
levels(our.data$repFT)[c(1, 3)] = c("0", "100")
levels(our.data$repFT)
levels(our.data$repFT)[28:30] = c("Dont Recognize", "Dont Know", "Not Ascertained")
levels(our.data$repFT)
our.data$repFT = car::recode(our.data$repFT, "'Not Ascertained' = NA;
                             'Dont Recognize' = NA; 
                             'Dont Know' = NA")
levels(our.data$repFT)
our.data$repFT = as.numeric(as.character(our.data$repFT))
table(our.data$repFT, our.data$V880165)
table(our.data$repFT)

# FEELING THERMOMETER, DEMOCRATIC PARTY. CORRECT CODE IS V880164
table(our.data$V880164)
levels(our.data$V880164)
our.data$demFT = our.data$V880164
levels(our.data$demFT)
levels(our.data$demFT)[c(1, 3)] = c("0", "100")
levels(our.data$demFT)
levels(our.data$demFT)[28:30] = c("Dont Recognize", "Dont Know", "Not Ascertained")
levels(our.data$demFT)
our.data$demFT = car::recode(our.data$demFT, "'Not Ascertained' = NA;
                             'Dont Recognize' = NA; 
                             'Dont Know' = NA")
levels(our.data$demFT)
our.data$demFT = as.numeric(as.character(our.data$demFT))
table(our.data$demFT, our.data$V880164)
table(our.data$demFT)

# DIFFERENCE, REPUBLICAN FT - DEMOCRATIC FT 
our.data$repversusdemdiff_therm = our.data$repFT - our.data$demFT
summary(our.data$repversusdemdiff_therm)
our.data$repversusdemdiff_therm = scales::rescale(our.data$repversusdemdiff_therm)
table(our.data$repversusdemdiff_therm)

# PARTY ID. CORRECT CODE IS V880274
table(our.data$V880274)
our.data$partyID = as.numeric(our.data$V880274)
table(our.data$partyID)
our.data$partyID = car::recode(our.data$partyID, "8 = NA; 9 = NA; 10 = NA")
our.data$partyID = our.data$partyID - 1
table(our.data$partyID, our.data$V880274)
# 0 = STRONG DEMOCRAT; 6 = STRONG REPUBLICAN

# REPUBLICAN
our.data$republican = ifelse(our.data$partyID>=4 & our.data$partyID<=6, 1, 0)
table(our.data$republican, our.data$V880274)

# DEMOCRAT 
our.data$democrat = ifelse(our.data$partyID>=0 & our.data$partyID<=2, 1, 0)
table(our.data$democrat, our.data$V880274)

# IDEOLOGICAL SELF-IDENTIFICATION. CORRECT CODE IS V880228. 
table(our.data$V880228)
our.data$ideology = as.numeric(our.data$V880228)
table(our.data$ideology)
our.data$ideology = car::recode(our.data$ideology, "1 = 5; 9 = 5; 10 = NA")
table(our.data$ideology)
our.data$ideology = our.data$ideology - 2
table(our.data$ideology, our.data$V880228)
# 0 = EXTREMELY LIBERAL; 6 = EXTREMELY CONSERVATIVE; 
# DK OR HTMA CODED AT MIDPOINT. NOTE: ADD FOOTNOTE TO THE ANALYSIS OF 1988, SAYING THAT 
# TO STANDARDIZE MEASUREMENT ACROSS 1988 AND 2020, WE REGARD RESPONDENTS' INITIAL 
# RESPONSE TO THE IDEOLOGICAL SELF-PLACEMENT QUESTION AS THEIR IDEOLOGICAL IDENTIFICATION.
# THE 2020 ANES DOES NOT INCLUDE FOLLOW-UPS. 

# CONSERVATIVE 
our.data$conservative = ifelse(our.data$ideology>=4 & our.data$ideology<=6, 1, 0)
table(our.data$conservative, our.data$V880228)

# LIBERAL
our.data$liberal = ifelse(our.data$ideology>=0 & our.data$ideology<=2, 1, 0)
table(our.data$liberal, our.data$V880228)

# SORTED
our.data$sorted = rep(0, length(our.data$ideology))
our.data$sorted[our.data$republican==1 & our.data$conservative==1] = 1
table(our.data$sorted)
our.data$sorted[our.data$democrat==1 & our.data$liberal==1] = 1
table(our.data$sorted)
our.data$sorted[is.na(our.data$partyID)=="TRUE" | is.na(our.data$ideology)=="TRUE"] = NA
table(our.data$sorted)
table(our.data$sorted[our.data$republican==1], our.data$ideology[our.data$republican==1])
table(our.data$sorted[our.data$democrat==1], our.data$ideology[our.data$democrat==1])
# 1 = CONSERVATIVE REPUBLICAN OR LIBERAL DEMOCRAT (SELF-IDENTIFIED); 0 = ALL OTHERS

# IDEOLOGICAL PLACEMENT, DEMOCRATIC PARTY. CORRECT CODE IS V880235.
# NOTE THAT INAPPLICABLE MEANS THAT THEY DID NOT PLACE THEMSELVES ON THE LIBERAL-CONSERVATIVE SPECTRUM
# EITHER INITIALLY *OR* IN THE FOLLOW-UP. 
table(our.data$V880235)
our.data$demplacement = as.numeric(our.data$V880235)
table(our.data$demplacement)
our.data$demplacement = car::recode(our.data$demplacement, "1 = NA; 9 = NA; 10 = NA")
our.data$demplacement = our.data$demplacement - 2
table(our.data$demplacement, our.data$V880235)
# 0 = EXTREMELY LIBERAL; 6 = EXTREMELY CONSERVATIVE 

# IDEOLOGICAL PLACEMENT, REPUBLICAN PARTY. CORRECT CODE IS V880234 
table(our.data$V880234)
our.data$GOPplacement = as.numeric(our.data$V880234)
table(our.data$GOPplacement)
our.data$GOPplacement = car::recode(our.data$GOPplacement, "1 = NA; 9 = NA; 10 = NA")
our.data$GOPplacement = our.data$GOPplacement - 2
table(our.data$GOPplacement, our.data$V880234)
# 0 = EXTREMELY LIBERAL; 6 = EXTREMELY CONSERVATIVE

# KNOWS PARTIES' POLICY REPUTATIONS
our.data$knowsreputations = ifelse((our.data$GOPplacement > our.data$demplacement), 1, 0)
sum(table(our.data$knowsreputations))
sum(table(our.data$knowsreputations, our.data$GOPplacement))
sum(table(our.data$knowsreputations, our.data$demplacement))
sum(table(our.data$knowsreputations, (our.data$GOPplacement - our.data$demplacement)))
# 'DK' CODED AS NOT KNOWING POLICY REPUTATIONS
our.data$knowsreputations[our.data$V880234=="8. DON-T KNOW"] = 0
table(our.data$knowsreputations)
our.data$knowsreputations[our.data$V880235=="8. DON-T KNOW"] = 0
sum(table(our.data$knowsreputations)) # N = 1781
# 0 = DOESN'T KNOW THAT THE REPUBLICAN PARTY IS MORE CONSERVATIVE THAN THE DEMOCRATIC PARTY;
# 1 = KNOWS THAT THE REPUBLICAN PARTY IS MORE CONSERVATIVE THAN THE DEMOCRATIC PARTY. 

# PROGRAMMATIC INDEX
our.data$programmatic = rep(NA, length(our.data$ideology))
# NOT MATCHED
our.data$programmatic[our.data$sorted==0 & our.data$knowsreputations==0] = 0
table(our.data$programmatic)
# PARTIALLY MATCHED
our.data$programmatic[our.data$sorted==1 & our.data$knowsreputations==0] = 0.5
table(our.data$programmatic)
our.data$programmatic[our.data$sorted==0 & our.data$knowsreputations==1] = 0.5
table(our.data$programmatic)
# MATCHED
our.data$programmatic[our.data$sorted==1 & our.data$knowsreputations==1] = 1
table(our.data$programmatic)
# CODE AS UNMATCHED PEOPLE THAT DIDN'T SEE THE PARTY PLACEMENT QUESTION BECAUSE THEY DIDN'T 
# PLACE THEMSELVES ON THE LIBERAL-CONSERVATIVE SPECTRUM
our.data$programmatic[our.data$V880228=="0. HAVEN-T THOUGHT" & our.data$V880234=="0. INAP."] = 0
table(our.data$programmatic)
our.data$programmatic[our.data$V880228=="8. DON-T KNOW" & our.data$V880234=="0. INAP."] = 0
table(our.data$programmatic)
# 1 = MATCHED; 0.5 = PARTIALLY MATCHED; 0 = UNMATCHED 

# SEVEN-POINT SCALE: GOVERNMENT SERVICES AND SPENDING. CORRECT CODE IS V880302
table(our.data$V880302)
our.data$govspending = as.numeric(our.data$V880302)
table(our.data$govspending)
our.data$govspending = car::recode(our.data$govspending, "1 = 5; 9 = 5; 10 = NA")
our.data$govspending = our.data$govspending - 2
table(our.data$govspending)
our.data$govspending = abs(our.data$govspending - 6)
our.data$govspending = scales::rescale(our.data$govspending)
table(our.data$govspending, our.data$V880302)
# 0-6 SCALE, 0 = GOVERNMENT SHOULD PROVIDE MANY MORE SERVICES; 6 = GOVERNMENT SHOULD PROVIDE
# MANY FEWER SERVICES. 'HAVEN'T THOUGHT MUCH ABOUT THIS' AND 'DK' CODED AT THE MIDPOINT. 

# SEVEN-POINT SCALE: GOVERNMENT HEALTHCARE. CORRECT CODE IS V880318
table(our.data$V880318)
our.data$govhealth = as.numeric(our.data$V880318)
table(our.data$govhealth)
our.data$govhealth = car::recode(our.data$govhealth, "1 = 5; 9 = 5; 10 = NA")
our.data$govhealth = our.data$govhealth - 2
our.data$govhealth = scales::rescale(our.data$govhealth)
table(our.data$govhealth, our.data$V880318)
# 0-TO-6 SCALE; 0 = GOVERNMENT-RUN HEALTH INSURANCE; 6 = PRIVATE INSURANCE
# 'HAVEN'T THOUGHT MUCH ABOUT THIS' AND 'DK' CODED AT THE MIDPOINT. 

# SEVEN-POINT SCALE: GOVERNMENT GUARANTEED JOBS. CORRECT CODE IS V880323
table(our.data$V880323)
our.data$govjobs = as.numeric(our.data$V880323)
table(our.data$govjobs)
our.data$govjobs = car::recode(our.data$govjobs, "1 = 5; 9 = 5; 10 = NA")
our.data$govjobs = our.data$govjobs - 2
our.data$govjobs = scales::rescale(our.data$govjobs)
table(our.data$govjobs, our.data$V880323)
# 0-TO-6 SCALE, 0 = GOVERNMENT SHOULD SEE TO JOBS AND GOOD STANDARD OR LIVING; 6 = 
# GOVERNMENT SHOULD LET EACH GET AHEAD ON OWN 

# ADDITIVE INDEX: GOVERNMENT CONSERVATISM
our.data$govconservatism = (our.data$govjobs + our.data$govhealth + our.data$govspending)/3
table(our.data$govconservatism)
our.data$govconservatism = scales::rescale(our.data$govconservatism)
table(our.data$govconservatism)

# POLITICAL KNOWLEDGE: PARTY WITH MOST MEMBERS IN HOUSE BEFORE ELECTION. CORRECT CODE IS 
# V880878. CORRECT ANSWER IS 'DEMOCRATS'
table(our.data$V880878)
our.data$house = our.data$V880878
our.data$house = as.numeric(our.data$house)
table(our.data$house)
our.data$house = car::recode(our.data$house, "1 = NA; 2 = 0; 3 = 1; 4 = 0; 5 = NA")
table(our.data$house, our.data$V880878)
# 1 = CORRECT; 0 = INCORRECT 

# POLITICAL KNOWLEDGE: PARTY WITH MOST MEMBERS IN SENATE BEFORE ELECTION
# CORRECT CODE IS V880879. CORRECT ANSWER IS 'DEMOCRATS'
table(our.data$V880879)
our.data$senate = our.data$V880879
our.data$senate = as.numeric(our.data$senate)
table(our.data$senate)
our.data$senate = car::recode(our.data$senate, "1 = NA; 2 = 0; 3 = 1; 4 = 0; 5 = NA")
table(our.data$senate, our.data$V880879)
# 1 = CORRECT; 0 = INCORRECT 

# POLITICAL KNOWLEDGE: ADDITIVE INDEX 
attach(our.data)
knowledge.matrix = data.frame(house, senate)
knowledge.matrix = as.matrix(knowledge.matrix)
head(knowledge.matrix)
our.data$politicalknowledge = rowSums(knowledge.matrix, na.rm = TRUE)
table(our.data$politicalknowledge)
summary(our.data$politicalknowledge)
detach(our.data)
# DIDN'T ANSWER BOTH QUESTIONS; DROP THESE FOLKS. 
table(our.data$politicalknowledge)
our.data$politicalknowledge[is.na(our.data$senate)=="TRUE" & 
                              is.na(our.data$house)=="TRUE"] = NA
table(our.data$politicalknowledge)
summary(our.data$politicalknowledge)
our.data$politicalknowledge = scales::rescale(our.data$politicalknowledge)
table(our.data$politicalknowledge)
# 0-TO-1 INDEX, % OF ANSWERS THAT THEY GOT CORRECT. NA's ARE ONLY FOR PEOPLE THAT DIDN'T ANSWER
# *EITHER* OF THE TWO QUESTIONS. 

# LOW-LEVEL ACTIVISM: TRY TO INFLUENCE OTHERS' VOTE CHOICES. CORRECT CODE IS V880825
table(our.data$V880825)
our.data$lla_1 = as.numeric(our.data$V880825)
table(our.data$lla_1)
our.data$lla_1 = car::recode(our.data$lla_1, "1 = NA; 2 = 1; 3 = 0; 4 = NA; 5 = NA")
table(our.data$lla_1, our.data$V880825)
# 1 = YES; 0 = NO

# LOW-LEVEL ACTIVISM: WEAR BUTTON, PUT STICKER ON CAR, OR PUT UP SIGN. CORRECT CODE IS 
# V880826
table(our.data$V880826)
our.data$lla_2 = as.numeric(our.data$V880826)
table(our.data$lla_2)
our.data$lla_2 = car::recode(our.data$lla_2, "1 = NA; 2 = 1; 3 = 0; 4 = NA; 5 = NA")
table(our.data$lla_2, our.data$V880826)
# 1 = YES; 0 = NO

# LOW-LEVEL ACTIVISM: ATTEND ANY POLITICAL MEETINGS OR RALLIES. CORRECT CODE IS V880827
table(our.data$V880827)
our.data$lla_3 = as.numeric(our.data$V880827)
table(our.data$lla_3)
our.data$lla_3 = car::recode(our.data$lla_3, "1 = NA; 2 = 1; 3 = 0; 4 = NA; 5 = NA")
table(our.data$lla_3, our.data$V880827)
# 1 = YES; 0 = NO

# LOW-LEVEL ACTIVISM: DO ANY (OTHER) WORK FOR PARTY OR CANDIDATE. CORRECT CODE IS V880828
table(our.data$V880828)
our.data$lla_4 = as.numeric(our.data$V880828)
table(our.data$lla_4)
our.data$lla_4 = car::recode(our.data$lla_4, "1 = NA; 2 = 1; 3 = 0; 4 = NA; 5 = NA")
table(our.data$lla_4, our.data$V880828)
# 1 = YES; 0 = NO

# LOW-LEVEL ACTIVISM: CONTRIBUTE MONEY TO SPECIFIC CANDIDATE. CORRECT CODE IS V880830
table(our.data$V880830)
our.data$lla_5a = as.numeric(our.data$V880830)
table(our.data$lla_5a)
our.data$lla_5a = car::recode(our.data$lla_5a, "1 = NA; 2 = 1; 3 = 0; 4 = NA; 5 = NA")
table(our.data$lla_5a, our.data$V880830)
# 1 = YES; 0 = NO

# LOW-LEVEL ACTIVISM: CONTRIBUTE MONEY TO SPECIFIC PARTY. CORRECT CODE IS V880832
table(our.data$V880832)
our.data$lla_5b= as.numeric(our.data$V880832)
table(our.data$lla_5b)
our.data$lla_5b = car::recode(our.data$lla_5b, "1 = NA; 2 = 1; 3 = 0; 4 = NA; 5 = NA")
table(our.data$lla_5b, our.data$V880832)
# 1 = YES; 0 = NO

# LOW-LEVEL ACTIVISM: CONTRIBUTE MONEY TO OTHER GROUP SUPPORTING/OPPOSING CANDIDATES. CORRECT CODE IS V880834
table(our.data$V880834)
our.data$lla_5c= as.numeric(our.data$V880834)
table(our.data$lla_5c)
our.data$lla_5c = car::recode(our.data$lla_5c, "1 = NA; 2 = 1; 3 = 0; 4 = NA; 5 = NA")
table(our.data$lla_5c, our.data$V880834)
# 1 = YES; 0 = NO

# LOW-LEVEL ACTIVISM: CONTRIBUTED MONEY, REGARDLESS OF RECIPIENT (AGGREGATION OF THREE VARIABLES LISTED ABOVE)
our.data$lla_5 = ifelse((our.data$lla_5a==1 | our.data$lla_5b==1 | our.data$lla_5c==1), 1, 0)
table(our.data$lla_5, our.data$V880834)
table(our.data$lla_5, our.data$V880832)
table(our.data$lla_5, our.data$V880830)
summary(our.data$lla_5)

# LOW-LEVEL ACTIVISM: ADDITIVE INDEX 
attach(our.data)
lla.data = data.frame(lla_1, lla_2, lla_3, lla_4, lla_5)
lla.data = as.matrix(lla.data)
head(lla.data)
our.data$llactivism = rowSums(lla.data, na.rm = TRUE)
summary(our.data$llactivism)
detach(our.data)
# DIDN'T ANSWER ANY OF THE QUESTIONS; DROP THESE PEOPLE.
table(our.data$llactivism)
our.data$llactivism[is.na(our.data$lla_1)=="TRUE" & 
                      is.na(our.data$lla_2)=="TRUE" & 
                      is.na(our.data$lla_3)=="TRUE" & 
                      is.na(our.data$lla_4)=="TRUE" & 
                      is.na(our.data$lla_5a)=="TRUE" & 
                      is.na(our.data$lla_5b)=="TRUE" & 
                      is.na(our.data$lla_5c)=="TRUE"] = NA
table(our.data$llactivism)
summary(our.data$llactivism)
our.data$llactivism = scales::rescale(our.data$llactivism)
table(our.data$llactivism)
# ADDITIVE INDEX, RESCALED BETWEEN 0-1. THE NAs ARE PEOPLE THAT DIDN'T ANSWER *ANY* OF THE 
# LOW-LEVEL ACTIVISM QUESTIONS (EITHER BECAUSE THEY SKIPPED THEM ALL OR DIDN'T TAKE THE 
# POST-ELECTION SURVEY)

# EDUCATIONAL ATTAINMENT (CONTINUOUS VARIABLE). CORRECT CODE IS V880422
table(our.data$V880422)
our.data$education = as.numeric(our.data$V880422)
table(our.data$education)
our.data$education = car::recode(our.data$education, "1 = 0; 2 = 0; 3 = 1; 4 = 2; 5 = 3; 6 = 4; 7 = 5; 8 = NA; 9 = NA")
table(our.data$education, our.data$V880422)
our.data$education = scales::rescale(our.data$education)
table(our.data$education, our.data$V880422)
# 0 = LESS THAN HIGH SCHOOL; 1 = HIGH SCHOOL OR EQUIVALENCY; 2 = SOME COLLEGE; 
# 3 = TWO-YEAR DEGREE; 4 = FOUR-YEAR DEGREE; 5 = POST-GRADUATE DEGREE

# INTEREST IN CAMPAIGN. CORRECT CODE IS V880097
table(our.data$V880097)
our.data$interestcampaigns = as.numeric(our.data$V880097)
table(our.data$interestcampaigns)
our.data$interestcampaigns = car::recode(our.data$interestcampaigns, "1 = 1; 2 = 0.5; 3 = 0; else = NA")
table(our.data$interestcampaigns, our.data$V880097)
# 0 = NOT INTERESTED; 0.5 = SOMEWHAT INTERESTED; 1 = VERY INTERESTED 

# HOW OFTEN DOES RESPONDENT FOLLOW GOVERNMENT OR PUBLIC AFFAIRS. ONLY ASKED ON THE POST-ELECTION MODULE. 
table(our.data$V880812)
our.data$newsinterest = as.numeric(our.data$V880812)
table(our.data$newsinterest)
our.data$newsinterest = car::recode(our.data$newsinterest, "1 = NA; 2 = 3; 3 = 2; 4 = 1; 5 = 0; else = NA")
table(our.data$newsinterest, our.data$V880812)
our.data$newsinterest = scales::rescale(our.data$newsinterest)
table(our.data$newsinterest, our.data$V880812)
# 0-TO-3 SCALE, 0 = HARDLY AT ALL; 1 = ONLY NOW AND THEN; 2 = SOME OF THE TIME; 3 = MOST OF THE TIME 

# INTEREST, ADDITIVE INDEX 
our.data$interest.politics = (our.data$interestcampaigns + our.data$newsinterest)/2
table(our.data$interest.politics)

# MORAL TRADITIONALISM: WE SHOULD ADJUST MORAL BEHAVIOR TO CHANGES IN THE WORLD
table(our.data$V880951)
our.data$adjustmorals = as.numeric(our.data$V880951)
table(our.data$adjustmorals)
our.data$adjustmorals = car::recode(our.data$adjustmorals, "1 = NA; 2 = 0; 3 = 1; 4 = 2; 5 = 3; 6 = 4; 7 = 2; else = NA")
table(our.data$adjustmorals, our.data$V880951)
our.data$adjustmorals = scales::rescale(our.data$adjustmorals)
table(our.data$adjustmorals, our.data$V880951)
# 0 = AGREE STRONGLY; 4 = DISAGREE STRONGLY. DK CODED AT THE MIDPOINT. 

# MORAL TRADITIONALISM: WE SHOULD BE MORE TOLERANT OF PEOPLE WITH DIFFERENT MORAL STANDARDS 
table(our.data$V880952)
our.data$toleratenew = as.numeric(our.data$V880952)
table(our.data$toleratenew)
our.data$toleratenew = car::recode(our.data$toleratenew, "1 = NA; 2 = 0; 3 = 1; 4 = 2; 5 = 3; 6 = 4; 7 = 2; else = NA")
table(our.data$toleratenew, our.data$V880952)
our.data$toleratenew = scales::rescale(our.data$toleratenew)
table(our.data$toleratenew, our.data$V880952)
# 0 = AGREE STRONGLY; 4 = DISAGREE STRONGLY. DK CODED AT THE MIDPOINT. 

# MORAL TRADITIONALISM: THERE WOULD BE FEWER PROBLEMS IF MORE EMPHASIS ON TRADITIONAL FAMILY TIES
table(our.data$V880953)
our.data$familyties = as.numeric(our.data$V880953)
table(our.data$familyties)
our.data$familyties = car::recode(our.data$familyties, "1 = NA; 2 = 4; 3 = 3; 4 = 2; 5 = 1; 6 = 0; 7 = 2; else = NA")
table(our.data$familyties, our.data$V880953)
our.data$familyties = scales::rescale(our.data$familyties)
table(our.data$familyties, our.data$V880953)
# 0 = DISAGREE STRONGLY; 4 = AGREE STRONGLY. DK CODED AT THE MIDPOINT. 

# MORAL TRADITIONALISM: NEWER LIFESTYLES CONTRIBUTING TO SOCIETAL BREAKDOWN 
table(our.data$V880954)
our.data$newerlifestyles = as.numeric(our.data$V880954)
table(our.data$newerlifestyles)
our.data$newerlifestyles = car::recode(our.data$newerlifestyles, "1 = NA; 2 = 4; 3 = 3; 4 = 2; 5 = 1; 6 = 0; 7 = 2; else = NA")
table(our.data$newerlifestyles, our.data$V880954)
our.data$newerlifestyles = scales::rescale(our.data$newerlifestyles)
table(our.data$newerlifestyles, our.data$V880954)
# 0 = DISAGREE STRONGLY; 4 = AGREE STRONGLY. DK CODED AT THE MIDPOINT. 

# MORAL TRADITIONALISM, ADDITIVE INDEX
our.data$tradvalues = (our.data$familyties + our.data$toleratenew + 
                        our.data$adjustmorals + our.data$newerlifestyles)/4
table(our.data$tradvalues)
summary(our.data$tradvalues)
tail(names(our.data))

# AID TO BLACKS. CORRECT CODE IS V880332 
table(our.data$V880332)
our.data$aidtoblacks = as.numeric(our.data$V880332)
table(our.data$aidtoblacks)
our.data$aidtoblacks = car::recode(our.data$aidtoblacks, "1 = 5; 9 = 5; 10 = NA")
table(our.data$aidtoblacks)
our.data$aidtoblacks = our.data$aidtoblacks - 2
our.data$aidtoblacks = scales::rescale(our.data$aidtoblacks)
table(our.data$aidtoblacks, our.data$V880332)
# 0 = GOVERNMENT SHOULD HELP BLACKS; 6 = GOVERNMENT SHOULD HELP THEMSELVES.
# 'HAVEN'T THOUGHT MUCH ABOUT THIS' AND 'DK RECODED AT THE MIDPOINT. 

# PREFERENTIAL HIRING FOR BLACKS
table(our.data$V880857)
our.data$aa_hiring = as.numeric(our.data$V880857)
table(our.data$aa_hiring)
our.data$aa_hiring = car::recode(our.data$aa_hiring, "1 = NA; 2 = 0; 3 = 1; 4 = 2; 5 = 3; else = NA")
table(our.data$aa_hiring, our.data$V880857)
our.data$aa_hiring = scales::rescale(our.data$aa_hiring)
table(our.data$aa_hiring, our.data$V880857)
# 0-TO-3 SCALE, 0 = FAVOR STRONGLY; 3 = OPPOSE STRONGLY 

# WHITE 
table(our.data$V880412)
our.data$white = as.numeric(our.data$V880412)
table(our.data$white)
our.data$white = car::recode(our.data$white, "1 = 1; 2 = 0; 3 = 0; 4 = 0; 5 = 0; 6 = NA")
table(our.data$white, our.data$V880412)
# 1 = WHITE; 0 = OTHER

# HISPANIC
table(our.data$V880539)
table(our.data$V880540)
our.data$hispanic = rep(0, length(our.data$white))
our.data$hispanic[our.data$V880539=="1. SOME HISPANIC GR"] = 1
table(our.data$hispanic)
our.data$hispanic[our.data$V880540=="1. YES"] = 1
table(our.data$hispanic)
table(our.data$hispanic, our.data$V880539)
table(our.data$hispanic, our.data$V880540)
# 1 = HISPANIC; 0 = NOT HISPANIC (INCLUDING DON'T KNOW AND NOT ASCERTAINED)

# WHITE, NON-HISPANIC
our.data$whiteNH = ifelse(our.data$white==1 & our.data$hispanic==0, 1, 0)
table(our.data$whiteNH)
sum(table(our.data$whiteNH))

# RACIAL ATTITUDES
our.data$racial = (our.data$aa_hiring + our.data$aidtoblacks)/2
table(our.data$racial)

# NUMBER OF ELIGIBLE ADULTS, 1988 SAMPLE. ("IN ANALYSIS, A RESPONDENT SELECTION WEIGHT 
# SHOULD BE USED TO COMPENSATE FOR THESE UNEQUAL SELECTION PROBABILITIES. THE VALUE
# OF THE RESPONDENT SELECTION WEIGHT IS EXACTLY EQUAL TO THE NUMBER OF ELIGIBLE ADULTS IN THE HOUSEHOLD
# FROM WHICH THE RANDOM RESPONDENT WAS SELECTED.")
table(our.data$V880091)
our.data$weight = as.numeric(our.data$V880091)
table(our.data$weight)
#-----
# DROP INDEPENDENTS FROM THE DATASET
new.data = subset(our.data, republican==1 | democrat==1)
#----
# SAVE DATASET 
# write.csv(new.data, file = "ANES1988_cleaned.csv")
